package com.wmlive.hhvideo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import butterknife.BindView;
import cn.wmlive.hhvideo.R;

/**
 * Created by lsq on 7/3/2017.
 * 首页底部的tab
 */

public class MainBottomTabView extends BaseCustomView {
    @BindView(R.id.ivHome)
    ImageView ivHome;
    @BindView(R.id.vHomeIndicator)
    View vHomeIndicator;
    @BindView(R.id.ivPublish)
    ImageView ivPublish;
    @BindView(R.id.ivMine)
    ImageView ivMine;
    @BindView(R.id.vMineIndicator)
    View vMineIndicator;
    private int index;

    public MainBottomTabView(Context context) {
        super(context);
    }

    public MainBottomTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void initViews(Context context, AttributeSet attrs, int defStyle) {
        ivHome.setOnClickListener(this);
        ivPublish.setOnClickListener(this);
        ivMine.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_main_bottom_tab;
    }

    @Override
    protected void onSingleClick(View v) {
        super.onSingleClick(v);
        if (null != onMainTabClickListener) {
            boolean isValid;
            switch (v.getId()) {
                case R.id.ivHome:
                    if (index != 0) {
                        isValid = onMainTabClickListener.onHomeClick();
                        if (isValid) {
                            setSelect(0);
                        }
                    }
                    break;
                case R.id.ivPublish:
                    onMainTabClickListener.onPublishClick();
                    break;
                case R.id.ivMine:
                    if (index != 2) {
                        isValid = onMainTabClickListener.onMineClick();
                        if (isValid) {
                            setSelect(2);
                        }
                    }
                    break;
            }
        }
    }

    public void setSelect(int position) {
        switch (position) {
            case 0:
                vHomeIndicator.setVisibility(VISIBLE);
                vMineIndicator.setVisibility(INVISIBLE);
                index = 0;
                break;
            case 2:
                vHomeIndicator.setVisibility(INVISIBLE);
                vMineIndicator.setVisibility(VISIBLE);
                index = 2;
                break;
        }
    }

    private OnMainTabClickListener onMainTabClickListener;

    public void setTabClickListener(OnMainTabClickListener onMainTabClickListener) {
        this.onMainTabClickListener = onMainTabClickListener;
    }
}
